from PyQt6.QtWidgets import (QWidget, QVBoxLayout, QLabel, 
                           QFrame, QScrollArea, QHBoxLayout, QPushButton)
from PyQt6.QtCore import Qt, pyqtSignal, QSize
from PyQt6.QtGui import QFont, QPixmap, QIcon
from ..view_setting import (LOGO,TITLE, TITLE_FRAME, NAV_TITLE,NAV_ICONS,NAV_BUTTON_STYLE,
                            COLORS, VERSION_INFO,)

class NavButton(QPushButton):
    """导航按钮组件"""
    def __init__(self, text, icon_path=None, parent=None):
        # 在文本前添加空格来增加图标与文字的间距
        text_with_spacing = "     " + text  # 使用空格增加间距
        super().__init__(text_with_spacing, parent)
        
        if icon_path:
            self.setIcon(QIcon(icon_path))
            self.setIconSize(QSize(NAV_BUTTON_STYLE["icon_size"], NAV_BUTTON_STYLE["icon_size"]))
        
        # 也可以尝试这个方法设置间距
        self.setStyleSheet(f"""
            QPushButton {{
                border: none;
                text-align: left;
                padding-left: {NAV_BUTTON_STYLE["padding_left"]}px;
                padding-right: 20px;
                padding-top: 10px;
                padding-bottom: 10px;
                color: {NAV_BUTTON_STYLE["text_normal"]};
                font-size: {NAV_BUTTON_STYLE["font_size"]};
                font-weight: {NAV_BUTTON_STYLE["font_weight"]};
            }}
            QPushButton:hover {{
                background-color: {NAV_BUTTON_STYLE["bg_hover"]};
                color: {NAV_BUTTON_STYLE["text_hover"]};
            }}
            QPushButton:checked {{
                background-color: {NAV_BUTTON_STYLE["bg_hover"]};
                color: {NAV_BUTTON_STYLE["text_hover"]};
                border-left: 3px solid {NAV_BUTTON_STYLE["accent"]};
            }}
        """)
        
        self.setCheckable(True)
        self.setAutoExclusive(True)
        self.setMinimumHeight(NAV_BUTTON_STYLE["min_height"])

class Sidebar(QWidget):
    """侧边栏组件"""
    page_changed = pyqtSignal(str)
    
    def __init__(self):
        super().__init__()
        self.setFixedWidth(250)
        self.setup_ui()
        
    def setup_ui(self):
        # 创建主布局
        layout = QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        
        # 创建顶部标题区域
        title_frame = QFrame()
        title_frame.setObjectName("title_frame")
        title_frame.setFixedHeight(70)
        title_frame.setStyleSheet(f"""
            #title_frame {{
                background-color: {TITLE_FRAME["TITLE_BG"]};
                border-bottom: 1px solid {TITLE_FRAME["BORDER"]};
            }}
        """)
        
        # 创建水平布局来放置图标和标题
        title_layout = QHBoxLayout(title_frame)
        title_layout.setContentsMargins(0, 0, 0, 0)
        title_layout.setSpacing(10)
        
        # 创建一个容器来包含图标和标题
        container = QWidget()
        container_layout = QHBoxLayout(container)
        container_layout.setContentsMargins(0, 0, 0, 0)
        container_layout.setSpacing(10)
        
        # 系统图标
        logo_label = QLabel()
        logo_pixmap = QPixmap(LOGO["LOGO_PATH"])
        logo_label.setPixmap(logo_pixmap.scaled(LOGO["LOGO_SIZE"], LOGO["LOGO_SIZE"], 
                                                Qt.AspectRatioMode.KeepAspectRatio, 
                                                Qt.TransformationMode.SmoothTransformation))
        logo_label.setFixedSize(LOGO["LOGO_SIZE"], LOGO["LOGO_SIZE"])
        container_layout.addWidget(logo_label)
        
        # 系统标题
        title = QLabel(TITLE["SYSTEM_NAME"])
        title.setFont(QFont(TITLE["family"], 
                          TITLE["size"]))
        title.setStyleSheet(f"color: {TITLE['color']};")
        container_layout.addWidget(title)
        
        # 将容器添加到标题布局，并设置居中对齐
        title_layout.addWidget(container, 0, Qt.AlignmentFlag.AlignCenter)
        
        layout.addWidget(title_frame)
        
        # 创建导航按钮区域（使用滚动区域）
        scroll = QScrollArea()
        scroll.setWidgetResizable(True)
        scroll.setFrameShape(QFrame.Shape.NoFrame)
        scroll.setStyleSheet("QScrollArea { border: none; }")
        
        nav_widget = QWidget()
        nav_layout = QVBoxLayout(nav_widget)
        nav_layout.setContentsMargins(0, 0, 0, 0)
        nav_layout.setSpacing(0)
        
        # 添加导航按钮
        self.NAV_TITLE = {}
        for key, text in NAV_TITLE.items():
            icon_path = NAV_ICONS.get(key)
            self.NAV_TITLE[key] = NavButton(text, icon_path)
        
        # 添加按钮到布局
        for name, button in self.NAV_TITLE.items():
            nav_layout.addWidget(button)
            button.clicked.connect(lambda checked, n=name: self.page_changed.emit(n))
        
        nav_layout.addStretch()
        scroll.setWidget(nav_widget)
        layout.addWidget(scroll)
        
        # 添加底部版本信息
        version_label = QLabel(VERSION_INFO["text"])
        version_label.setStyleSheet(f"""
            color: {VERSION_INFO["color"]};
            font-size: 12px;
            padding: 15px;
        """)
        version_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(version_label)
        
        # 设置整体样式
        self.setStyleSheet(f"""
            QWidget {{
                background-color: {COLORS["MAIN_BG"]};
            }}
        """) 